local Msg = require("core.Msg")
local ActivityExcel = require("excel.Activity")
local CopyExcel = require("excel.Map").copy
local MonsterExcel = require("excel.Monster").monster
local ActivityLogicL = require("activity.ActivityLogicL")
local CopyHandler = require("copy.HandlerL")
local Grid = require("bag.Grid")
local CopyCommon = require("copy.CopyCommon")
local CopyL = require("copy.CopyL")
local ItemDefine = require("bag.ItemDefine")
local PfLogic = require("tencentApi.PfLogic")
ActValueRewardCache = ActValueRewardCache or {}    -- 活跃度奖励道具grid缓存 避免频繁new grid

CacheReward = CacheReward or {}
function getRewardGrid(tb, id, itemid, cnt, bind, quality)
    tb[id] = tb[id] or {}
    if tb[id][itemid] == nil then
        tb[id][itemid] = {}
        Grid.create(tb[id][itemid], itemid, cnt, bind, quality)
    end
    return tb[id][itemid]
end

function getRewardGridCnt(tb, id)
    tb[id] = tb[id] or {}
    local cnt = 0
    for _, v in pairs(tb[id]) do
        cnt = cnt + 1
        CacheReward[cnt] = v
    end
    return cnt, CacheReward
end

function CG_ACTIVITY_VALUE_CONF(human)
    local msg = Msg.gc.GC_ACTIVITY_VALUE_CONF
    local len = 0
    for k, v in pairs(ActivityExcel.actValue) do
        len = len + 1
        msg.acts[len].id = k
        msg.acts[len].name = v.name
        msg.acts[len].valuePerCnt = v.valuePerCnt
        msg.acts[len].sort = v.sortIndex
        msg.acts[len].cmd = v.cmd
        msg.acts[len].yb = v.yuanbao
        for i = 1, #v.args do
            msg.acts[len].args[i] = v.args[i] .. ""
        end
        msg.acts[len].args[0] = #v.args
        msg.acts[len].rewardDesc = v.rewardDesc
        msg.acts[len].lv = v.lv
    end
    msg.acts[0] = len
    len = 0

    for k, v in pairs(ActivityExcel.actValueReward) do
        len = len + 1
        msg.rewards[len].value = v.value
        local itemLen = 0
        for kk, vv in pairs(v.reward) do
            itemLen = itemLen + 1
            Grid.makeItem(getRewardGrid(ActValueRewardCache, k, vv[1], vv[2], vv[3], vv[4]), msg.rewards[len].reward[itemLen], 0, human)
            if itemLen == 3 then
                break
            end
        end
        msg.rewards[len].reward[0] = itemLen
    end
    msg.rewards[0] = len
    Msg.send(msg, human.fd)
end

function CG_ACTIVITY_VALUE_UPDATE(human)
    local len = 0
    local msg = Msg.gc.GC_ACTIVITY_VALUE_UPDATE
    for k, v in pairs(ActivityExcel.actValue) do
        len = len + 1
        msg.acts[len].id = k
        msg.acts[len].curCnt = ActivityLogicL.getActValueRecordCnt(human, k)
        msg.acts[len].maxCnt = v.maxCnt
    end
    msg.acts[0] = len
    len = 0
    for k, v in pairs(ActivityExcel.actValueReward) do
        len = len + 1
        msg.rewards[len].value = v.value
        if human.db.actValue < v.value then
            msg.rewards[len].status = 2
        elseif v.value > human.db.actValueRewardOk then
            msg.rewards[len].status = 1
        else
            msg.rewards[len].status = 0
        end
    end
    msg.rewards[0] = len
    msg.actvalue = human.db.actValue
    Msg.send(msg, human.fd)
end

function CG_ACTIVITY_VALUE_REWARD(human)
    ActivityLogicL.getActValueReward(human)
end

function CG_ACTIVITY_COPY_CONF(human)
    local msg = Msg.gc.GC_ACTIVITY_COPY_CONF
    local len = 0
    for k in ipairs(ActivityExcel.copy) do
        local v = ActivityExcel.copy[k]
        local copy = CopyExcel[v.copyID]
        if copy then
            len = len + 1
            msg.copys[len].id = k
            msg.copys[len].copyID = v.copyID
            msg.copys[len].type = copy.type
            local group = CopyL.group2Copy[copy.group]
            for i = 1, #group do
                local config = CopyExcel[group[i]]
                msg.copys[len].copyIDs[i] = group[i]
                msg.copys[len].lvs[i] = config.level
                msg.copys[len].difficult[i] = config.difficult            
            end
            msg.copys[len].copyIDs[0] = #group
            msg.copys[len].lvs[0] = #group
            msg.copys[len].difficult[0] = #group
            msg.copys[len].name = copy.name
            msg.copys[len].icon = copy.icon
            msg.copys[len].thumb = copy.thumb
            msg.copys[len].desc1 = copy.desc
            msg.copys[len].desc2 = copy.desc2
            msg.copys[len].mapID = v.mapID
            msg.copys[len].vipTips = v.vipTips
            msg.copys[len].vipAddCost = v.vipAddCost
        end
    end
    msg.copys[0] = len
    Msg.send(msg, human.fd)
end

function CG_ACTIVITY_COPY_REWARD(human, copyID)
    local msg = Msg.gc.GC_ACTIVITY_COPY_REWARD
    local copy = CopyExcel[copyID]
    if not copy then 
        return
    end
    msg.items[0] = 0
    local dropYB = PfLogic.isAgentTencent()
    local itemCount, itemGrids = CopyHandler.getRewardItem(copyID)
    for i = 1, itemCount do
        if dropYB or (itemGrids[i].id ~= ItemDefine.ITEM_ID_DROP_YUANBAO and itemGrids[i].id ~= ItemDefine.ITEM_ID_YUANBAO) then  
          msg.items[0] = msg.items[0] + 1   
          Grid.makeItem(itemGrids[i], msg.items[i], 0)
          if i == 10 then
              break
          end
        end
    end
    msg.copyID = copyID
    Msg.send(msg, human.fd)
end

function CG_ACTIVITY_COPY_UPDATE(human)
    local msg = Msg.gc.GC_ACTIVITY_COPY_UPDATE
    local len = 0
    for k, v in pairs(ActivityExcel.copy) do
        local copy = CopyExcel[v.copyID]
        if copy then
            len = len + 1
            msg.copys[len].id = k
            msg.copys[len].curCnt = CopyCommon.getCopyEnterCount(human, v.copyID)
            msg.copys[len].maxTotalCnt = copy.enterCount + CopyCommon.getCopyVipAddCnt(human, v.copyID)
            msg.copys[len].canAddCnt = ActivityLogicL.getCopyEnterCanAdd(human, v.copyID)
        end
    end
    msg.copys[0] = len
    Msg.send(msg, human.fd)
end


function  CG_ACTIVITY_ACT_CONF(human)
    local msg = Msg.gc.GC_ACTIVITY_ACT_CONF
    local len = 0
    for k, v in pairs(ActivityExcel.todayAct) do
        len = len + 1
        msg.activitys[len].id = k
        msg.activitys[len].actType = v.actType
        msg.activitys[len].name = v.name
        msg.activitys[len].icon = v.icon
        msg.activitys[len].thumb = v.thumb
        msg.activitys[len].lv = v.lv
        msg.activitys[len].desc1 = v.desc1
        msg.activitys[len].desc2 = v.desc2
        msg.activitys[len].timeStr = v.timeStr
        msg.activitys[len].mapID = v.mapID
        msg.activitys[len].items[0] = 0
    end
    msg.activitys[0] = len
    Msg.send(msg, human.fd)
end

function CG_ACTIVITY_ACT_UPDATE(human)
    local msg = Msg.gc.GC_ACTIVITY_ACT_UPDATE
    local len = 0
    for k, v in pairs(ActivityExcel.todayAct) do
        len = len + 1
        msg.acts[len].id = k
        msg.acts[len].actType = v.actType
        msg.acts[len].status = 1
        msg.acts[len].icon = v.icon
        msg.acts[len].leftTime = 0
    end
    msg.acts[0] = len
    Msg.send(msg, human.fd)
end
